#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "SharedMemory.h"

#define MEM_SIZE (50)

/*  Process 1 creates the shared memory, lock , input to it a string taken from command line if given   *
 *  else by defualt, and then go sleep for 30 seconds. When wake up unlock and deatach                 */
int main(int argc, char** argv)
{
    SM*     sm      = SMCreate(MEM_SIZE , "smtest4");
    int     i                                       ;
    int     sec = 20                                ;
    void*   memory  = SMGet(sm)                     ;

    printf("Process1: Lock operation: %d\n",SMLock(sm));

    if(argc > 2)
    {
        *((char*)memory) = '\0';
        for(i = 1; i < argc; ++i)
        {
            strcat((char*)memory, argv[i]);
            strcat((char*)memory, " ");
        }
    }
    else                /*  defualt text    */
    {
        strcpy((char*)memory, "This is a test for Shared Memory");
    }

    printf("Process1: sleep for %d seconds\n",sec);
    sleep(sec);

    printf("Process1: Unlock operation: %d\n",SMUnlock(sm));
    sleep(5);

    SMDetach(sm);

    return 0;
}
